home *** CD-ROM | disk | FTP | other *** search
/ Scene Storm / Scene Storm - Volume 1.iso / coding / c / unixdirsii / source / adjust.c next >
Encoding:
C/C++ Source or Header  |  1995-11-05  |  2.2 KB  |  119 lines

  1. /*
  2.  * Routine to convert a UNIX-style path to an AmigaDOS path.
  3.  * A UNIX-style path is understood here as follows:
  4.  * 
  5.  *     leading . means current dir; like "", but filled in explicitly
  6.  *     ../ means /
  7.  *     ..  means /
  8.  * 
  9.  * Written as part of UnixDirs2, a (to be written) system patch which allows
  10.  * use of UNIX-style paths everywhere.
  11.  * 
  12.  * Martin W. Scott,  8 January 1993
  13.  */
  14. #include <exec/types.h>
  15. #include <exec/execbase.h>
  16. #include <dos/dos.h>
  17. #include <dos/dosextens.h>
  18. #include <string.h>
  19. #include <proto/dos.h>
  20. #include <proto/exec.h>
  21.  
  22. extern struct ExecBase *SysBase;
  23.  
  24. /* insert $cwd into s, return pointer to next free char */
  25. char *
  26. insertcwd(char *s, LONG len)
  27. {
  28.     geta4();
  29.  
  30.     if (NameFromLock(((struct Process *)(SysBase->ThisTask))->pr_CurrentDir, s, len))
  31.     {
  32.         while (*s)
  33.             s++;
  34.     }
  35.     return s;
  36. }
  37.  
  38. /* adjust path from UNIX-style to Amiga-style */
  39. /* TO DO: length checking when building new path */
  40. BOOL
  41. adjustpath(char *path, char *newpath, LONG len)
  42. {
  43.     char *s, *t;
  44. #ifdef DEBUG
  45.     char *origpath = path;
  46. #endif
  47.     geta4();
  48.     s = newpath;
  49.  
  50.     if (path == NULL)    /* bypass */
  51.         return FALSE;
  52.  
  53.     if (t = strchr(path, ':'))        /* check for ':' in path */
  54.     {
  55.         t++;                /* copy device component */
  56.         while (path < t)
  57.             *s++ = *path++;
  58.     }
  59.     else if (path[0] == '.')        /*** translate '.' to $cwd ***/
  60.     {
  61.         if (!path[1])            /* only "." */
  62.         {
  63.             s = insertcwd(s, len);
  64.             path++;            /* path[0] == '\0' - STOP */
  65.         }
  66.         else if (path[1] == '/')    /* initial component is $cwd */
  67.         {
  68.             s = insertcwd(s, len);
  69.             if (*(s-1) != ':')
  70.                 *s++ = '/';    /* copy '/', increment pointers */
  71.             path += 2;
  72.         }
  73.     }
  74.  
  75.     while (path[0])            /*** copy remainder of path ***/
  76.     {
  77.         if (path[0] == '.' && path[1] == '.')
  78.         {
  79.             if (path[2] == '/')    /* just skip "..", copying '/' */
  80.             {
  81.                 path += 2;
  82.                 *s++ = *path++;
  83.             }
  84.             else if (!path[2])    /* append '/' and stop */
  85.             {
  86.                 path += 2;
  87.                 *s++ = '/';
  88.             }
  89.             else *s++ = *path++;
  90.         }
  91.         else *s++ = *path++;
  92.     }
  93.     *s = '\0';
  94.  
  95.     return TRUE;
  96. }
  97.  
  98. #ifdef TEST
  99.  
  100. void
  101. main(int argc, char **argv)
  102. {
  103.     UWORD i;
  104.  
  105.     for (i = 1; i < argc; i++)
  106.     {
  107.         char *buf;
  108.  
  109.         if (buf = AllocMem(512, 0L))
  110.         {
  111.             adjustpath(argv[i], buf, 512);
  112.             printf("%s --> %s\n", argv[i], buf);
  113.             FreeMem(buf, 512);
  114.         }
  115.     }
  116.  
  117. } /* main */
  118.  
  119. #endif